<?php
class orders extends spController{
	public function __construct() {
		//登录验证	
		if(!isset($_SESSION['IS_LOGIN']) || !$_SESSION['IS_LOGIN']){
			$this->jump (spUrl ('login','index'));
		}
		parent::__construct();
		$this -> _controller = 'orders';
		$this -> _pk = spClass('MOrders') -> pk;
		$this -> _attributes = spClass('MOrders') -> getAttributes();
		$this -> _attributeFormset = spClass('MOrders') -> getAttributeFormsets();
		$this -> savesession = $_SESSION['IS_LOGIN'];
		$this -> savesessionid = $_SESSION['LOGIN_ID'];
		spAddViewFunction('cbForm',array('LibCodeBuilder','cbFormWidget'));
		$this->spAcl = spClass("spAcl")->get();
	}
	
	public function index(){
		$this->search();
	}
	
	public function view(){
		$this->_attributes = spClass('MOrders') -> getDetailAttributes();
		$condition = array();
		
		if($this -> spArgs('id')) $condition[$this->_pk] = $this -> spArgs('id');
		if($this -> spArgs('orderId')) $condition['order_id'] = $this -> spArgs('orderId');

		$this -> tpl_data = spClass('MOrders') -> find($condition);
		$this -> display('orders/view.html');
	}
	
	public function create(){
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');
			
			//内部处理的
			$rows['from_user_id'] = $_SESSION['LOGIN_ID'];
			$rows['from_user_name'] = $_SESSION['IS_LOGIN'];
			$rows['orderflag'] = 0;
			$rows['type_id'] = 1;
			$rows['status'] = 7;
			$rows['order_id'] = $rows['tel'];
			$rows['tel'] = '';
			$rows['order_time'] = date('Y-m-d H:i:s');
			$rows['ip'] = spGetip();
			$rows['refer'] = $_SERVER['HTTP_REFERER'];
			$rows['order_page'] = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
			
			if(($id = spClass('MOrders')->create($rows)))
				$this ->success ('新增数据成功',  spUrl ($this->_controller, 'view', array('id'=>$id)));
			else
				$this ->error ('新增数据失败', 'javascript:history.go(-1)');
		}
		
		$this->allproduces = spClass('MProduce') -> findAll();
		
		$this ->display('orders/create.html');
	}
	
	public function update(){
		$attrubutesss = spClass('MOrders') -> getDetailAttributes();
		if($this->spAcl != 'ADMIN'){
			$attrubutesss = array(
				'good_name' => '订单名',
				'daishoukuan' => '代收货款',
				'receiver_name' => '收货人',
				'tel' => '电话',
				'address' => '收货地址',
				'is_duanxin' => '是否短信',
				'wuliu' => '物流',
				'comments' => '备注',
			);
		}
	
		$this->_attributes = $attrubutesss;		
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');
			if(spClass('MOrders')->update($condition,$rows))
				//$this ->success ('编辑数据成功',  spUrl ($this->_controller, 'view', array('id'=>$this -> spArgs('id'))));
				$this ->success ('编辑数据成功',  spUrl ($this->_controller, 'search'));
			else
				$this ->error ('编辑数据失败', 'javascript:history.go(-1)');
		}
		$this -> tpl_data = spClass('MOrders') -> find($condition);
		$this ->display('orders/update.html');
	}
	
	public function delete(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');

		$success = spClass('MOrders') -> delete($condition);
		if($success)
			$this ->success('删除成功', $this->spArgs('redirect','javascript:history.go(-1)'));
		else
			$this ->error('删除失败', $this->spArgs('redirect','javascript:history.go(-1)'));
	}
	
	public function search(){
		$attributes = spClass('MOrders') -> getUnFilteAttributes();
		
		if($_POST){
			$this -> tpl_keyword = $conditions = spClass('spArgs')->get($this->_controller, null, 'post');
			$this -> tpl_searchType = $searchType = spClass('spArgs')->get($this->_controller.'_searchType', null, 'post');
			$where = array();
			foreach($conditions as $key => $value){
				if(!$value) continue;
				//if(empty($this->_attributes[$key])) continue;
				if($searchType[$key] == 1) $where[] = ' '.$key.' LIKE '.spClass('MOrders')->escape($value).' ';
				else $where[] = ' '.$key.' LIKE '.spClass('MOrders')->escape('%'.$value.'%').' ';
			}
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$allorders = spClass('MOrders') -> spPager($page, 10) -> findAll(implode('AND',$where), " id DESC  ");
			$this -> tpl_datas =  $allorders;
			$this -> tpl_pager = spClass('MOrders') -> spPager() -> getPager();
			
			if($_POST['qubie'] == 2){
				$this->daochuexcel($allorders);
				exit;
			}
		}else{
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MOrders') -> spPager($page, 10) -> findAll();
			$this -> tpl_pager = spClass('MOrders') -> spPager() -> getPager();
		}
		
		$this->_attributes = $attributes;
		$this -> display('orders/search.html');
	}
	
	public function searchuidorder(){
		$this->_attributes = spClass('MOrders') -> getUnFilteAttributes();
		$where = array();
		$conditions = array();
		
		foreach($conditions as $key => $value){
			if(!$value) continue;
			//if(empty($this->_attributes[$key])) continue;
			if($searchType[$key] == 1) $where[] = ' '.$key.' LIKE '.spClass('MOrders')->escape($value).' ';
			else $where[] = ' '.$key.' LIKE '.spClass('MOrders')->escape('%'.$value.'%').' ';
		}
		$this -> tpl_page = $page = $this -> spArgs('page',1);
		$allorders = spClass('MOrders') -> spPager($page, 30) -> findAll(implode('AND',$where), " id DESC  ");
		$this -> tpl_datas =  $allorders;
		$this -> tpl_pager = spClass('MOrders') -> spPager() -> getPager();
		
		$this -> display('orders/search.html');

	}
	
	public function mainlist(){		
		$allorders = spClass('MOrders') -> findAll();
		
		$curtime = strtotime(date("Y-m-d"));
		$oldtime1 = $curtime - 3600*24;
		
		$oldtime2 = $curtime - 3600*24*7;
		$oldtime3 = $curtime - 3600*24*30;
		
		$fahuonum = $fahuonum2 = $qsumall = $qsum2 = $qsum3 = 0;
		$benzhouqianshounum = 0;
		$benyueqianshounum = 0;
		$totalqianshouum = 0;
		$daichuli = 0;
		foreach($allorders as $data){
			if(($data['status'] == 4 || $data['status'] == 6) && strtotime($data['delivery_time']) >= $oldtime1 && strtotime($data['delivery_time']) < $curtime){
				$fahuonum ++;
			}
			
			if(($data['status'] == 4 || $data['status'] == 6) && strtotime($data['delivery_time']) >= $curtime){
				$fahuonum2 ++;
			}
			
			if($data['status'] == 6){
				if(strtotime($data['qianshoutime']) >= $oldtime2){
					$qsum2 ++;
				}
				
				if(strtotime($data['qianshoutime']) >= $oldtime3){
					$qsum3 ++;
				}
				
				$qsumall ++;
			}
			
			if($data['status'] == 7){
				$daichuli ++ ;
			}
		}
		
		$allorders = count($allorders);
		$this->fahuonum = $fahuonum;
		$this->fahuonum2 = $fahuonum2;
		$this->qsum2 = number_format($qsum2 / $allorders,2);
		$this->qsum3 = number_format($qsum3 / $allorders,2);
		$this->qsumall = number_format($qsumall / $allorders,2);

		$this -> allogs = spClass('MLog') -> findAll($condition, 'logintime DESC limit 8');
		
		//待处理的单子  今天的发货量  昨天的发货量
		$this-> daichuli = $daichuli;
		$this -> display('orders/orderadmindetail.html');
	}
	
	//导出全部订单
	public function daochuexcel($allorders){
		header("Content-Type: application/force-download");
		header("Content-Type: application/octet-stream");
		header("Content-Type: application/download");
		header("Content-Disposition:attachment;filename=orderSystem.xls");//导出excel名设置
		header("Pragma: no-cache");
		header("Expires: 0");

		$allmysqlkeys = spClass('MOrders') -> getDetailAttributes();
		$title = array_values($allmysqlkeys);
		$keys = array_keys($allmysqlkeys);
		
		foreach($title as $val){
			echo @iconv('UTF-8','gbk//IGNORE',$val);
			echo "\t";
		}

		$data = array();
		if($allorders){
			foreach($allorders as $kk => $vv){
				foreach($keys as $key){
					$data[$kk][] = $vv[$key];
				}
			}
		}else{
			$data = array(array('暂无订单记录！'));
		}

		//输出标题
		echo "\n";

		//输出数据
		for($i=0;$i<count($data);$i++){
			foreach($data[$i] as $val){
				echo @iconv('UTF-8','gbk//IGNORE',$val);
				echo "\t";
			}
			echo "\n";
		}
		exit;//必须不能省略
	}
}